<script setup>
    import orderApi from "@/api/order.js";
    import categoryApi from "@/api/catetory.js";
    import WangEditor from "@/components/WangEditor.vue";
    import {ref} from "vue";
    import {ElMessage, ElMessageBox} from "element-plus";
    import {
        Delete,
        Edit,
        Upload,
        Download,
        Plus
    } from '@element-plus/icons-vue'
    import router from "@/router/index.js";
    //数据表格
    const list = ref([])
    const total = ref(0)
    //分页信息和搜索条件
    const orderQuery = ref({
        name: '',
        page: 1,
        limit: 10
    })
    const dateTimeRange = ref([])

    /*orderApi.list(orderQuery.value).then(result => {
        list.value = result.data.records
        total.value = result.data.total
    })*/

    const loadData = () => {
        orderApi.list(orderQuery.value).then(result => {
            if (result.code == 0) {
                list.value = result.data.records
                total.value = result.data.total
            }
        })
    }

    loadData()

    const onSearch = () => {
        orderQuery.value.page = 1
        orderQuery.value.beginCreateTime = null
        orderQuery.value.endCreateTime = null
        if(dateTimeRange.value && dateTimeRange.value.length == 2) {
            orderQuery.value.beginCreateTime = dateTimeRange.value[0]
            orderQuery.value.endCreateTime = dateTimeRange.value[1]
        }
        loadData()
    }

    const reset = () => {
        orderQuery.value.orderNo = ''
        dateTimeRange.value = []
    }

    //根据id删除
    const deleteById = (id) => {
        ElMessageBox.confirm(
            '您确认要删除么?',
            '警告',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        ).then(() => {
            orderApi.deleteById(id).then(result => {
                if (result.code == 0) {
                    ElMessage.success(result.msg)
                    loadData()
                } else {
                    ElMessage.error(result.msg)
                }
            })
        })
    }

    //批量删除
    const ids = ref([])
    const handleSelectionChange = (rows) => {
        console.log('多选', rows)
        ids.value = rows.map(function (row) {
            return row.id;
        })
        console.log(ids.value)
    }

    const deleteAll = () => {
        ElMessageBox.confirm(
            '您确认要删除么?',
            '警告',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        ).then(() => {
            orderApi.deleteAll(ids.value).then(result => {
                if (result.code == 0) {
                    ElMessage.success(result.msg)
                    loadData()
                } else {
                    ElMessage.error(result.msg)
                }
            })
        })
    }

    //展示订单详情
    const dialogFormVisible = ref(false)
    const orderItemList = ref([])
    const showOrderDetail = (orderNo) => {
        dialogFormVisible.value = true
        orderApi.selectOrderItemByOrderNo(orderNo).then(result => {
            orderItemList.value = result.data
        })
    }

    //模拟发货
    const sendProduct = (orderNo) => {
        const order = {
            orderNo: orderNo,
            status: 3
        }
        orderApi.update(order).then(result => {
            loadData()
        })
    }

</script>

<template>
    <el-card class="">
        <template #header>
            <div class="header">
                <el-button type="primary" :icon="Delete" @click="deleteAll">批量删除</el-button>
            </div>
        </template>
        <el-form :inline="true">
            <el-form-item label="名字">
                <el-input v-model="orderQuery.orderNo" placeholder="请输入订单号" clearable  style="width: 200px"/>
            </el-form-item>
            <el-form-item label="更新时间" :label-width="80">
                <el-date-picker
                    clearable
                    v-model="dateTimeRange"
                    type="datetimerange"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    value-format="YYYY-MM-DD HH:mm:ss"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSearch">搜索</el-button>
                <el-button type="warning" @click="reset">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="list" style="width: 100%" ref="multipleTableRef" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="orderNo" label="订单编号"/>
            <el-table-column prop="userId" label="用户id"/>
            <el-table-column prop="payment" label="总金额"/>
            <el-table-column prop="createTime" label="创建时间" width="180px"/>
            <el-table-column prop="paymentTime" label="付款时间时间" width="180px"/>
            <el-table-column prop="status" label="订单状态" width="180px">
                <template #default="{ row }">
                    <el-tag type="primary" v-if="row.status == 0">已取消</el-tag>
                    <el-tag type="warning" v-if="row.status == 1">未付款</el-tag>
                    <el-tag type="success" v-if="row.status == 2">已付款</el-tag>
                    <el-tag type="info" v-if="row.status == 3">已发货</el-tag>
                    <el-tag type="danger" v-if="row.status == 4">已发货</el-tag>
                </template>
            </el-table-column>
            <el-table-column align="center" width="200px" fixed="right" label="操作">
                <template #default="{ row }">
                    <el-button size="small" v-if="row.status==2" type="success" @click="sendProduct(row.orderNo)">发货</el-button>
                    <el-button size="small" type="danger" @click="showOrderDetail(row.orderNo)">订单详情</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            v-model:current-page="orderQuery.page"
            v-model:page-size="orderQuery.limit"
            :page-sizes="[10, 20, 30, 40]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @change="loadData"
            style="margin-top: 20px; justify-content: flex-end"
        />
    </el-card>

    <!--添加、编辑弹出框-->
    <el-dialog v-model="dialogFormVisible" :title="订单详情" width="70%" :lock-scroll="false">
        <el-table :data="orderItemList" style="width: 100%" >
            <el-table-column prop="orderNo" label="订单编号"/>
            <el-table-column prop="productId" label="用户id"/>
            <el-table-column prop="productName" label="商品名"/>
            <el-table-column prop="productImage" label="主图">
                <template #default="{ row }">
                    <img :src="row.productImage" style="max-height: 40px; max-width: 120px;"/>
                </template>
            </el-table-column>
            <el-table-column prop="productPrice" label="商品价格"/>
            <el-table-column prop="count" label="商品数量"/>
            <el-table-column prop="totalPrice" label="总金额"/>
        </el-table>
    </el-dialog>
</template>

<style scoped>
    .avatar-uploader .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }

    .avatar-uploader .el-upload:hover {
        border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        text-align: center;
    }
</style>